import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { MenuItem, TreeNode, AutoComplete, Message } from 'primeng/primeng';
import { flyIn } from '../../common/animation/common.animation';
import { UnitService } from './service/unit.service';

@Component({
  templateUrl: './unit.component.html',
  styleUrls: ['./unit.component.scss'],
  providers: [],
  animations: [flyIn]
})
export class UnitComponent implements OnInit {
  loading: boolean = true;
  units: TreeNode[];
  selectedUnit: TreeNode;
  suggestions: any[];
  suggestion: any;
  constructor(
    private unitService: UnitService,
    private route: ActivatedRoute,
    private router: Router) { }

  ngOnInit() {
    //初始化树控件
    this.initUnitInfoTree();
    //监听子组件中添加或删除事件，刷新树控件列表
    this.unitService.routeMsgPush$.subscribe(data => {
      for (var prop in data) {
        data = data[prop]
      }
      this.recursionRefreshTreeNode(this.units, data)
    });
  }
  /**初始化树控件 */
  initUnitInfoTree() {
    this.unitService.getUnitInfoTree(0, true).subscribe(data => {
      this.loading = false;
      this.units = data;
      if (this.units[0]) {
        this.selectedUnit = this.units[0];
        this.unitSelect(null);
      }
    });
  }
  /**树节点选中事件 */
  unitSelect(event) {
    this.router.navigate(['system/unit/detail', { "id": this.selectedUnit["id"] }]);
  }

  /**查询选中事件 */
  selectSuggestion() {
    this.recursionExpendSuggestionTreeNode(this.units, this.suggestion.unitCode);
  }

  /** 树节点展开事件 */
  unitNodeExpand(event) {
    if (event.node && event.node.children.length == 0) {
      this.unitService.getUnitInfoTree(event.node.id, false).subscribe(nodes => event.node.children = nodes);
    }
  }

  /**查询事件 */
  search(event) {
    this.unitService.searchUnitByName(event.query).subscribe(data => {
      data.forEach(ele => ele["unitName"] = ele["unitName"] + "(" + ele["unitId"] + ")")
      this.suggestions = data
    });
  }

  /**递归处理树节点的变更信息 **/
  recursionRefreshTreeNode(units: TreeNode[], unit: any) {
    units.forEach((node, i) => {
      if (node['id'] == unit.seniorId) {
        this.unitService.getUnitInfoTree(node['id'], false).subscribe(nodes => node.children = nodes);
      }
      return this.recursionRefreshTreeNode(node.children, unit);
    })
  }
  /**递归展开查询的树 */
  recursionExpendSuggestionTreeNode(units: TreeNode[], unitCode: string) {
    units.forEach(ele => {
      if (unitCode.indexOf('/' + ele['id'] + '/') != -1) {
        if (unitCode == ele.data['code']) {
          this.selectedUnit = ele;
          this.router.navigate(['system/unit/detail', { "id": this.selectedUnit["id"] }]);
          return
        }
        //展开树节点
        ele.expanded = true;
        if (ele.children.length > 0) {
          this.recursionExpendSuggestionTreeNode(ele.children, unitCode);
        } else {
          this.unitService.getUnitTree(+ele['id'], false).subscribe(nodes => {
            ele.children = nodes;
            this.recursionExpendSuggestionTreeNode(ele.children, unitCode);
          });
        }
      } else {
        //关闭非节点
        ele.expanded = false;
      }
    })
  }

}
